package frameData;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import message.Message;
import data.mainFrame;
import data.subFrame;
import dataBase.courseBaseConnector;
import factory.CourseFactory;

public class FrameData {
	
	courseBaseConnector cbc;
	CourseFactory cf;
	public FrameData(){
		cbc = new courseBaseConnector();
		cf = new CourseFactory();
	}

	public Message addFrame(mainFrame mf,subFrame[] sf) {
		// TODO Auto-generated method stub
		boolean label1 = cbc.addMainFrame(mf.getInfo()).getIsSuccess();
		if(label1){
			ArrayList<Integer> list = new ArrayList<Integer>();
			for(int i = 0;i<sf.length;i++){
				boolean lable2 = cbc.addSubFrame(sf[i].getInfo()).getIsSuccess();
				if(!lable2){
					list.add(i+1);
				}
			}
			if(list.size()==0)
				return new Message(true,null);
			else{
				StringBuilder sb = new StringBuilder();
				sb.append("fail to add ");
				for(int i = 0;i<list.size();i++){
					sb.append(sf[i].getCoursename()+" ");
				}
				return new Message(false,sb.toString());
			}
		}
		else
			return new Message(false,"fail to add");
	}
	
	public Message deleteFrame(mainFrame mf){
		
		cbc.deleteMainFrame(mf.getSerialNumber());
		cbc.deleteSubFrame("serialNumber", mf.getSerialNumber());
		
		return new Message(true,"succeed!");
		
	}
	
	public ArrayList<mainFrame> findMainFrame(){
		ArrayList<mainFrame> result = new ArrayList<mainFrame>();
		ArrayList<String[]> mInfoList = cbc.queryMainFrame();
		for(int i = 0;i<mInfoList.size();i++){
			mainFrame mf = cf.mainFrameMaker(mInfoList.get(i));				
			result.add(mf);
		}
		return result;
	}
	
	public subFrame[] findSubFraem(String serialNumber){
		ArrayList<String[]> sInfoList = cbc.querySubFrame("serialNumber", serialNumber);
		subFrame[] sb = null; 
		if(sInfoList.size()==0){
			sb = new subFrame[1];
			sb[0] = new subFrame("", "", "");
		}
		else{
			sb = new subFrame[sInfoList.size()];
			for(int j = 0;j<sInfoList.size();j++)
				sb[j] = cf.subFrameMaker(sInfoList.get(j));
		}
		
		return sb;
	}
	
	public Message update(mainFrame om,subFrame[] os,mainFrame mf,subFrame[] sf){
		// TODO Auto-generated method stub
	
		
		
		cbc.deleteMainFrame(om.getSerialNumber());
		cbc.deleteSubFrame("serialNumber", om.getSerialNumber());
		Message m3 = cbc.addMainFrame(mf.getInfo());
		if(!m3.getIsSuccess()){
			cbc.addMainFrame(om.getInfo());
			for(int i = 0;i<os.length;i++){
				cbc.addSubFrame(os[i].getInfo());
			}
			return new Message(false,"fail to update");
		}
		boolean label = true;
		int i = 0;
		for(i=0;i<sf.length;i++){
			label = label&&cbc.addSubFrame(sf[i].getInfo()).getIsSuccess();
			if(!label)
				break;
		}
		if(!label){
			cbc.deleteMainFrame(mf.getSerialNumber());
			for(int j = 0;j<i;j++)
				cbc.deleteSubFrame(sf[j].getCoursename());
			cbc.addMainFrame(om.getInfo());
			for(int k = 0;k<os.length;i++){
				cbc.addSubFrame(os[i].getInfo());
			}
			return new Message(false,"fail to update");
		}
		return new Message(true,"succeed!");
	}
	
	public Message submit(ArrayList<String> plan, String dp){
		// TODO Auto-generated method stub
		boolean result = true;
		File f = new File("data/plan/" + dp+".txt");
		if(!f.exists()){
			try {
				f.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			f.delete();
			f.createNewFile();
			BufferedWriter bw = new BufferedWriter(new FileWriter(f,true));
			for(int i = 0;i<plan.size();i++){
				bw.write(plan.get(i)+"\r\n");
			}
			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = false;
		}	
		
		
		return new Message(result,null);
	}
	public ArrayList<String> downLoad(String dp){
		// TODO Auto-generated method stub
		ArrayList<String> result = new ArrayList<String>();
		File f = new File("data/plan/" + dp+".txt");
		if(!f.exists()){
			String s = "file does not exist!";
			result.add(s);
		}
		else{
			try {
			@SuppressWarnings("resource")
			BufferedReader br = new BufferedReader(new FileReader(f));
			String line = null;
			while((line=br.readLine())!=null){
				result.add(line);	
			}
			 
		} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		return result;
	}
}
